package com.tro.common.util.easyui;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class Pagination<T> {

	private List<T> rows;

	private static final Integer DEFAULT_SIZE = 20;

	private Integer offset; // 偏移量
	
	private Integer limitEnd;

	public Integer getLimitEnd() {
		if(page==null){
			page=1;
		}
		if(size==null){
			size=20;
		}
		return page*size;
	}

	public void setLimitEnd(Integer limitEnd) {
		this.limitEnd = limitEnd;
	}

	private Integer total; // 总记录数

	private Integer page; // 当前页数

	private Integer size;

	private Object queryParams; // 查询参数

	private boolean isAll = false;// 数据权限:是否查询所有记录(默认查询自己权限内的数据)

	private Long operator;

	public Long getOperator() {
		return operator;
	}

	public void setOperator( Long operator ) {
		this.operator = operator;
	}

	public boolean getIsAll() {
		return isAll;
	}

	public void setIsAll( boolean isAll ) {
		this.isAll = isAll;
	}

	public Pagination() {
	}

	

	public Integer getOffset() {

		if ( offset == null || offset < 0 ) {
			offset = ( page - 1 ) * size;
		}
		if ( offset == null || offset < 0 ) {
			offset = 0;
		}

		return offset;
	}

	
	

	public void setOffset( Integer offset ) {
		this.offset = offset;
	}

	public Integer getTotal() {

		if ( total == null ) {
			total = DEFAULT_SIZE;
		}

		return total;
	}

	public void setTotal( Integer total ) {
		this.total = total;
	}

	public String getLimit() {
		return String.format( " limit %s, %s ", getOffset(), getTotal() );
	}

	public Integer getPage() {

		if ( page == null || page < 0 ) {
			page = 0;
		}

		return page;
	}

	public void setPage( Integer page ) {
		this.page = page;
	}

	public Integer getSize() {

		if ( size == null || size < 1 ) {
			if ( total == null || total == 0 )
				size = DEFAULT_SIZE;
			else
				size = total;
		}

		return size;
	}

	public void setSize( Integer size ) {
		this.size = size;
	}

	

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}


	public Object getQueryParams() {
		return queryParams;
	}

	public void setQueryParams( Object queryParams ) {
		this.queryParams = queryParams;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString( this, ToStringStyle.SHORT_PREFIX_STYLE );
	}

}
